﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Centaline.Common
{
  public static class StringUtil
    {

        /// <summary>
        /// Ajax提交表单编码数据解码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Decode(this string value)
        {
            if (value != null) value = value.Replace("%2B", "_$％2B$_");
            value = HttpUtility.UrlDecode(HttpUtility.UrlDecode(value));
            if (!string.IsNullOrEmpty(value))
            {
                value = value.Replace("_$％2B$_", "+");
            }
            return value;
        }
        /// <summary>
        /// Ajax提交表单编码数据解码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string DecodeAndReplaceQuotes(this string value)
        {
            if (value != null) value = value.Replace("%2B", "_$％2B$_");
            value = HttpUtility.UrlDecode(HttpUtility.UrlDecode(value));
            if (!string.IsNullOrEmpty(value))
            {
                //value = value.Replace("'", "＇"); 
                value = value.Replace("_$％2B$_", "+");

                //add by mayan ，前台拼字符串转码修正后注释该方法
                value = FilterSpecilChar(value); //update by hequanzhou ，去掉特殊字符以适应前台Grid的异常
                // value = value.Replace("'", "");
            }
            return value;
        }

        //add by Mayan 吃掉特殊字符； 前台拼字符串转码修正后注释该方法
        public static string FilterSpecilChar(String src)
        {
            StringBuilder sb = new StringBuilder(src);
            {
                //过滤url参数字符
                //过滤sql通配符
                //sb.Replace("&", ""); //前台拼字符串转码修正后注释该方法 by linhaixia
                sb.Replace("%", "％");
                sb.Replace("*", "＊");
                sb.Replace("'", "＇");
                sb.Replace("&", "＆");
                sb.Replace("\\", "＼");
                sb.Replace("\"", "＂");
                sb.Replace("+", "＋");
                sb.Replace("<", "＜");
                sb.Replace(">", "＞");

            }
            return sb.ToString();
        }

        //add by Mayan 吃掉特殊字符； 前台拼字符串转码修正后注释该方法
        public static string FilterSpecilCharForJson(String src)
        {
            StringBuilder sb = new StringBuilder(src);
            {
                //过滤url参数字符
                //过滤sql通配符
                //sb.Replace("&", ""); //前台拼字符串转码修正后注释该方法 by linhaixia
                sb.Replace("%", "％");
                sb.Replace("*", "＊");
                sb.Replace("'", "＇");
                sb.Replace("&", "＆");
                sb.Replace("\\\\", "＼");
                sb.Replace("\\\"", "＂");
                sb.Replace("+", "＋");
                sb.Replace("<", "＜");
                sb.Replace(">", "＞");

            }
            return sb.ToString();
        }


        /// <summary>
        /// Ajax提交表单编码数据编码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Encode(this string value)
        {
            if (string.IsNullOrEmpty(value)) return "";
            return HttpUtility.UrlEncode(HttpUtility.UrlEncode(value));
        }

        /// <summary>
        /// Ajax提交表单编码数据解码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string DecodeRelace(this string value)
        {

            return HttpUtility.UrlDecode(value).Replace("%2b", "+");
        }

        /// <summary>
        /// Ajax提交表单编码数据编码,将 "+" 替换成 "%2b"
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string EncodeRelace(this string value)
        {
            return HttpUtility.UrlEncode(value).Replace("+", "%2b"); ;
        }

        #region Html字符串编码、解码
        /// <summary>
        /// 返回 HTML 字符串的编码结果
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>编码结果</returns>
        public static string HtmlEncode(this string str)
        {
            if (str == null)
                return null;
            return str.Replace(" ", "&nbsp;").Replace("\r\n", "<br />");
        }

        /// <summary>
        /// 返回 HTML 字符串的解码结果
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns>解码结果</returns>
        public static string HtmlDecode(this string str)
        {
            if (str == null)
                return null;
            return str.Replace("&nbsp;", " ").Replace("<br />", "\r\n");
        }
        #endregion
    }
}
